<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>首页</title>
    <style>
        body {
            margin: 0 20px;
            line-height: 1;
        }

        a {
            text-decoration: none;
        }

        .title-wrapper {
            padding: 15px 0;
            border-bottom: 2px solid purple;
        }

        .title-wrapper .title {
            font-size: 20px;
            font-weight: bold;
        }

        .title-wrapper .info-wrapper span,
        .title-wrapper .info-wrapper a {
            font-size: 14px;
            color: #999;
        }
    </style>
</head>

<body>
    <h1>博客首页</h1>
    <div id="blog-container">

    </div>

    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
    <script src="https://cdn.bootcss.com/moment.js/2.23.0/moment.min.js"></script>
    <script src="https://cdn.bootcss.com/moment.js/2.23.0/locale/zh-cn.js"></script>
    <script>
        // async hronous  javascript and xml




        // 发送请求
        function get(url) {
            return $.get(url)
        }   

        // 显示时间格式化 
        function getFormatDate(dt) {

            return moment(Number(dt)).format('LL');
        }

        // 获取url中的的参数 
        function getUrlParams() {
            let paramsStr = location.href.split('?')[1] || '';
            paramsStr = paramsStr.split('#')[0]
            const result = {};
            // [name=zhangsa,age=20]
            paramsStr.split('&').forEach(itemStr => {
                // ['name','zhangsa']
                const arr = itemStr.split('=');
                const key = arr[0]
                const val = arr[1];
                result[key] = val
            })
            //  result = {author:'zhansan'}
            return result
        }

        // 获取dom元素 
        const $container = $('#blog-container')

        // 拼接接口 url  
        /* 
        http://www.localhost:5000/ap9/blog/index.html?name=&age
        */
        let url = '/api/blog/list';
        console.log(get("/api/blog/list") );
        // urlParams = {author:'zhansan'}
        const urlParams = getUrlParams();
        if (urlParams.author) {
            url += '?author=' + urlParams.author
        }
        var xhr = new XMLHttpRequest()

        // 加载数据了 
        get(url).then(res => {
            if (res.error !== 0) {
                alert('数据错误')
                return
            }
            // 遍历博客列表，并显示数据到页面
            const data = res.data || [];
            data.forEach(item => {
                // item就是数组中的每一个对象(渲染的数据) 
                $container.append(
                    $(`
                    <div class="title-wrapper">
            <p class="title">
                <a href="/detail.html?id=${item.id}">${item.title}</a>
            </p>
            <div class="info-wrapper">
                <span>
                  <a href="/admin.html?author=${item.author}">${item.author}</a>
                </span>
                <span>${getFormatDate(item.createtime)}</span>
            </div>
        </div>
                    `)
                )
            })
        })
    </script>
</body>

</html>